package com.sz.biz.common.project.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.constants.SystemConstants;
import com.sz.biz.common.customer.entity.CusCustomer;
import com.sz.biz.common.customer.service.CustomerService;
import com.sz.biz.common.platform.entity.PlatformCompany;
import com.sz.biz.common.platform.service.PlatformCompanyService;
import com.sz.biz.common.project.dto.ProjectCompanyDto;
import com.sz.biz.common.project.entity.ProjectCompany;
import com.sz.biz.common.project.service.ProjectCompanyService;
import com.sz.biz.common.project.service.ProjectService;
import com.sz.biz.common.sup.dto.SupplierDto;
import com.sz.biz.common.sup.service.SupplierService;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.system.service.OrgService;
import com.sz.common.core.system.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Function: TODO: ADD FUNCTION <br>
 * Author: qiankun<br>
 * Date: 2017-07-13 09:51:00
 */
@Service("ProjectCompanyService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class ProjectCompanyServiceImpl extends AbstractService implements ProjectCompanyService {
    private static final String IS_AUDIT = "isAudit";
    @Autowired
    private SupplierService supplierService;
    @Autowired
    private ProjectService projectService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private PlatformCompanyService platformCompanyService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrgService orgService;

    @Override
    public int save(ProjectCompany company) {
        checkMainDataValid(company);
        dao.save("ProjectCompanyMapper.insert",company);
        return company.getId();
    }

    private void checkMainDataValid(ProjectCompany company) {
        if (company != null) {
            if (StringUtils.isEmpty(company.getCompanyId()) || company.getCompanyId() == 0) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "companyId");
            }
            if (StringUtils.isEmpty(company.getType()) || company.getType() == 0) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "type");
            }
            if (StringUtils.isEmpty(company.getProjectId()) || company.getProjectId() == 0) {
                throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectId");
            }
        }
    }

    @Override
    public int update(ProjectCompany company) {
        if (StringUtils.isEmpty(company.getId()) || company.getId() == 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "id");
        }
        checkMainDataValid(company);
        int id = (int) dao.update("ProjectCompanyMapper.update", company);
        return id;
    }

    @Override
    public void delete(int id) {
        if (id <= 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "id");
        }
        dao.delete("ProjectCompanyMapper.delete", id);
    }

    @Override
    public void batchDelete(List<Integer> ids) {
        if (!CollectionUtils.isEmpty(ids)) {
            dao.batchDelete("ProjectCompanyMapper.delete", ids);
        }
    }

    @Override
    public void deleteByProjectId(int project) {
        if (project <= 0) {
            throw Exceptions.bizException(ErrorCodes.ERROR_ARGUMENT_EMPTY, "projectid");
        }
        dao.delete("ProjectCompanyMapper.deleteByProjectId",project);
    }

    @Override
    public List<ProjectCompany> queryByProjectId(int project) {
        List<ProjectCompany> listResult = null;
        if (project != 0) {
            listResult = dao.findForList("ProjectCompanyMapper.queryByProjectId", project, ProjectCompany.class);
        }
        return listResult;
    }

    @Override
    public QResultDto findByFilter(QueryParams queryParams) {
        ParamData paramData = this.convertQueryParams(queryParams);
        if (paramData.containsKey(IS_AUDIT) &&  ((Boolean) paramData.get(IS_AUDIT)) == true) {
            if (!isShowAll()) {
                paramData.put("isAudit", false);
            }
        }
        else {
            paramData.put("isAudit", false);
        }
        List<ProjectCompanyDto> list = new ArrayList<>();
        List<ProjectCompany> companys = dao.findForList("ProjectCompanyMapper.findByFilterListPage", paramData, ProjectCompany.class);
        if (!CollectionUtils.isEmpty(companys)) {
            companys.forEach(company -> {
                ProjectCompanyDto companyDto = new ProjectCompanyDto();
                BeanUtils.copyProperties(company, companyDto);
                if (company.getType() == SystemConstants.CompanyType.SUPPLIER ) {
                    SupplierDto sup = supplierService.findById(company.getCompanyId());
                    companyDto.setCompanyName(sup.getName());
                }
                if (company.getType() == SystemConstants.CompanyType.CUSTOMER ) {
                    CusCustomer customer = customerService.findSimpleById(company.getCompanyId());
                    companyDto.setCompanyName(customer.getUserName());
                }
                //平台
                if (company.getType() == SystemConstants.CompanyType.PLATFORM ) {
                    PlatformCompany pcompany = platformCompanyService.getPlatformCompanyById(company.getCompanyId());
                    companyDto.setCompanyName(pcompany.getName());
                }
                list.add(companyDto);
            });
        }
        return new QResultDto(list, paramData.getPagination());
    }

    private boolean isShowAll(){
        Map<String, Object> map = projectService.userProjectPermission();
        return (Boolean)map.get("isInApprovalGroup");
    }
}
